/**
 * 
 */
package org.tuestudent.grouppa;

import org.json.JSONObject;

/**
 * @author s121170 Class containing a single message from server
 */
public class MessageFromServer {

    /**
     * serialized in JSON arguments for the application
     */
    public String serializedArguments;
    /**
     * id of the message (maintained by server)
     */
    public long id;
    /**
     * id of the message to which this one is response (given by application)
     */
    public long responseTold;
    /**
     * which controller on the server sent this message
     */
    public String fromController;
    /**
     * to which controller this message is intended
     */
    public String toController;
    /**
     * to which task on the above controller is this intended
     */
    public String toTask;

    /**
     * used for identification of this response
     */
    public String messageType;

    /**
     * is the response good news?
     */
    public boolean statusOK;

    /**
     * Creates a new messageFromServer from the provided jsonobject in which it
     * is encoded
     * 
     * @param jobject
     *            json object with encoded message from server
     * @return new instance of MessageFromServer
     */
    public static MessageFromServer fromJSONObject(JSONObject jobject) {
        // CHECK: create a new MessageFromServer from provided json object
        MessageFromServer result = new MessageFromServer();
        result.fromController = jobject.optString("fromController");
        result.id = jobject.optLong("id");
        result.responseTold = jobject.optLong("responseToId");
        result.serializedArguments = jobject.optString("serializedArguments");
        result.toController = jobject.optString("toController");
        result.toTask = jobject.optString("toTask");
        result.statusOK = jobject.optBoolean("statusOK");
        result.messageType = jobject.optString("messageType");
        return result;
    }
}
